<?php

namespace app\beautiful\model\order;
;
use app\common\model\CommonModel;

use app\beautiful\model\member\MemberModel;
use think\facade\Db;

class OrderRefundModel extends CommonModel{

    protected  $name = 'beautiful_order_refund';

    protected  $pk   = 'order_refund_id';

    public static function onBeforeInsert($model)
    {
        $model->set("add_time", date('Y-m-d H:i:s'));
        $model->set("add_ip", request()->ip());
    }
    public static function onBeforeUpdate($model)
    {
        $model->set("update_time", date('Y-m-d H:i:s'));
    }
    public static function getById($where,$field='*')
    {
        $list = Db::name('beautiful_order_refund')->where($where)->field($field)->find();
        if($list) $list = $list->toArray();
        return $list;
    }

    public static function list($where,$order,$page=1,$limit=10){
        $query = Db::name('beautiful_order_refund')->alias('or')
            ->join('beautiful_order o','or.order_id=o.order_id')
            ->join('beautiful_member m','m.member_id=o.member_id')
            ->join('beautiful_order_item oi','or.order_id=oi.order_id')
            ->where(['or.is_delete'=>0])
            ->when(!empty($where['shop_id']),function ($query) use ($where){
                $query->where(['or.shop_id'=>$where['shop_id']]);
            })
            ->when(isset($where['status']),function ($query) use ($where){
                $query->where(['or.status'=>$where['status']]);
            })
            ->when(!empty($where['order_type']),function ($query) use ($where){
                $query->where(['o.order_type'=>$where['order_type']]);
            })
            ->when(!empty($where['order_sn']),function ($query) use ($where){
                $query->where('o.order_sn','like','%'.$where['order_sn'].'%');
            })
            ->when(!empty($where['mobile']),function ($query) use ($where){
                $query->where('m.mobile','like','%'.$where['mobile'].'%');
            })
            ->when(!empty($where['goods_name']),function ($query) use ($where){
                $query->where('oi.goods_name','like','%'.$where['goods_name'].'%');
            })
            ->when(!empty($where['member_id']),function ($query) use ($where){
                $query->where(['o.member_id'=>$where['member_id']]);
            })
            ->field("or.*,o.order_sn,o.order_type,o.need_pay,o.pay_type,o.mix_pay_type,o.platform_money,o.money,o.weixin,m.member_num,m.face,m.nick_name as member_name,oi.goods_name")
            ->group('or.order_refund_id');
        $count=$query->count();
        $list=$query->order($order)
            ->limit($limit)->page($page)
            ->select()
            ->toArray();
        if(!empty($list)){
            foreach ($list as $k=>$v){
                $list[$k]['pay_type_mean'] = payType($v);
            }
        }
        return compact('count','list');
    }

}



